/*
 分析: 判断是 leftDetailView 还是 rightDetailView 上的 "+" 被点击了.  --> 怎么判断??
        
        leftDetailView  和 rightDetailView 都有 "+" ---> increaseLabel
        
        启示: 怎么知道 是 leftView 还是 rightView 被点击了??  --> 添加手势,传递不同的
              index
        
        --->  动画是 有谁来实现的??? ---> 控制器 
 
 
 */

#import "EGNewYearCell.h"
#import "EGDetailView.h"

typedef NS_ENUM(NSInteger,EGNewYearCellType) {
    
    Horizontal,
    Vertical
};


@interface EGNewYearCell()

@property (strong,nonatomic) UIImageView *iv_newYear;

@property (strong,nonatomic) UIView *leftView;
@property (strong,nonatomic) UIView *rightView;

@property (strong,nonatomic) UIImageView *leftImageView;
@property (strong,nonatomic) UIImageView *rightImageView;


@property (strong,nonatomic) EGDetailView *leftDetailView;
@property (strong,nonatomic) EGDetailView *rightDetailView;

@property (assign,nonatomic)EGNewYearCellType type;

@end



@implementation EGNewYearCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor colorWithRed:0.902 green:0.902 blue:0.902 alpha:1.0];
        
        [self setupUI];
    }
    return self;
}

#pragma mark - 赋值
- (void)setHomeModel:(EGHomeModel *)homeModel{
    
    _homeModel = homeModel;
    
    self.type = Horizontal;
    
    if (homeModel.customURL != nil) {
        
        [self.iv_newYear sd_setImageWithURL:[NSURL URLWithString:homeModel.img]];
    }else{
        //给 leftView, rightView 赋值
        //取出 img
        self.iv_newYear.hidden = YES;
    }
}


#pragma mark - 赋值
- (void)setFreshArray:(NSArray<EGFreshModel *> *)freshArray{
    
    _freshArray = freshArray;
    //赋值
    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:freshArray.firstObject.img]];
    
    [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:freshArray.lastObject.img]];
    
    
    self.leftDetailView.currentPriceLabel.text = freshArray.firstObject.price;
    self.leftDetailView.prePriceLabel.text = freshArray.firstObject.market_price;
    
    self.rightDetailView.currentPriceLabel.text = freshArray.lastObject.price;
    self.rightDetailView.prePriceLabel.text = freshArray.lastObject.market_price;
    
    self.leftDetailView.nameLabel.text = freshArray.firstObject.name;
    self.rightDetailView.nameLabel.text = freshArray.lastObject.name;
    
    self.leftDetailView.weightLabel.text = freshArray.firstObject.specifics;
    self.rightDetailView.weightLabel.text = freshArray.lastObject.specifics;
    
    self.leftDetailView.prePriceLabel.text = freshArray.firstObject.market_price;
    self.rightDetailView.prePriceLabel.text = freshArray.lastObject.market_price;
    
    if ([self.leftDetailView.prePriceLabel.text isEqualToString:self.leftDetailView.currentPriceLabel.text]) {
        
        self.leftDetailView.prePriceLabel.hidden = YES;
    }else{
        
        self.leftDetailView.prePriceLabel.hidden = NO;
    }
    
    if ([self.rightDetailView.prePriceLabel.text isEqualToString:self.rightDetailView.currentPriceLabel.text]) {
        
        self.rightDetailView.prePriceLabel.hidden = YES;
    }else{
        
        self.rightDetailView.prePriceLabel.hidden = NO;
    }
}

#pragma mark - 轻拍方法的实现
- (void)leftTap:(UITapGestureRecognizer *)tap{
    
    if (self.cellBlock) {
        
        self.cellBlock(self.freshArray,1);
    }
}

#pragma mark - 轻拍方法的实现
- (void)rightTap:(UITapGestureRecognizer *)tap{
    
    if (self.cellBlock) {
        
        self.cellBlock(self.freshArray,2);
    }
}


- (void)setupUI{
    
    //添加控件
    [self.contentView addSubview:self.leftView];
    [self.contentView addSubview:self.rightView];
    
    [self.contentView addSubview:self.iv_newYear];

    [self.leftView addSubview:self.leftImageView];
    [self.rightView addSubview:self.rightImageView];
    
    [self.leftView addSubview:self.leftDetailView];
    [self.rightView addSubview:self.rightDetailView];
    
    
    [self.iv_newYear makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(UIEdgeInsetsZero);
    }];
    
    [self.leftDetailView makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo(self.leftView);
        make.top.equalTo(self.leftImageView.mas_bottom);
    }];

    [self.rightDetailView makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo(self.rightView);
        make.top.equalTo(self.rightImageView.mas_bottom);
    }];

    
    [self.leftView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.leading.equalTo(self.contentView).offset(5);
        make.bottom.equalTo(self.contentView).offset(-5);
        make.trailing.equalTo(self.contentView.centerX).offset(-5);
    }];
    
    [self.rightView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView).offset(5);
        make.bottom.trailing.equalTo(self.contentView).offset(-5);
        make.leading.equalTo(self.contentView.centerX).offset(5);
    }];
    
    [self.leftImageView makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(UIEdgeInsetsMake(0, 0, 70, 0));
    }];
    
    [self.rightImageView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsMake(0, 0, 70, 0));
    }];
}

- (UIImageView *)iv_newYear{
    
    if (_iv_newYear == nil) {
        
        _iv_newYear = [[UIImageView alloc]init];
    }
    return _iv_newYear;
}

- (UIView *)leftView{
    
    if (_leftView == nil) {
        
        _leftView = [[UIView alloc]init];
        
        UITapGestureRecognizer *tapGesturre = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(leftTap:)];
        
        [_leftView addGestureRecognizer:tapGesturre];
        
        _leftView.backgroundColor = [UIColor whiteColor];
    }
    return _leftView;
}

- (UIView *)rightView{
    
    if (_rightView == nil) {
        
        _rightView = [[UIView alloc]init];
        
        UITapGestureRecognizer *rightGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(rightTap:)];
        
        [_rightView addGestureRecognizer:rightGesture];

        _rightView.backgroundColor = [UIColor whiteColor];
    }
    return _rightView;
}

- (UIImageView *)leftImageView{
    
    if (_leftImageView == nil) {
        
        _leftImageView = [[UIImageView alloc]init];
    }
    return _leftImageView;
}

- (UIImageView *)rightImageView{
    if (_rightImageView == nil) {
        _rightImageView = [[UIImageView alloc]init];
        
    }
    return _rightImageView;
}

- (EGDetailView *)leftDetailView{
    
    if (_leftDetailView == nil) {
        
        _leftDetailView = [[EGDetailView alloc]init];
        
        [_leftDetailView.increaseButton addTarget:self action:@selector(leftIncreaseButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        [_leftDetailView.reduceButton addTarget:self action:@selector(leftReduceButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _leftDetailView;
}


#pragma mark - 监听方法
- (void)leftIncreaseButtonClick{
    
    self.leftDetailView.count++;
    
    //要判断 是 left 还是 right 被点击了
    self.leftDetailView.countLabel.text = [NSString stringWithFormat:@"%ld",self.leftDetailView.count];
    
    if (self.animationBlcok) {
        
        self.animationBlcok(self.leftImageView);
    }

}

- (void)leftReduceButtonClick{
    
    if (self.leftDetailView.count > 0) {
        
        self.leftDetailView.count--;
        
        self.leftDetailView.countLabel.text = [NSString stringWithFormat:@"%ld",self.leftDetailView.count];
    }
}

- (void)rightReduceButtonClick{
    
    if (self.rightDetailView.count > 0) {
        
        self.rightDetailView.count--;
        
        self.rightDetailView.countLabel.text = [NSString stringWithFormat:@"%ld",self.rightDetailView.count];
    }
}


- (void)rightIncreaseButtonClick{
    
    self.rightDetailView.count++;
    
    //要判断 是 left 还是 right 被点击了
    self.rightDetailView.countLabel.text = [NSString stringWithFormat:@"%ld",self.rightDetailView.count];
    
    //执行动画   ---> 传入 imageView  和 frame
    
    if (self.animationBlcok) {
        
        self.animationBlcok(self.rightImageView);
    }
  
}

- (EGDetailView *)rightDetailView{
    
    if (_rightDetailView == nil) {
        
        _rightDetailView = [[EGDetailView alloc]init];
        
        [_rightDetailView.increaseButton addTarget:self action:@selector(rightIncreaseButtonClick) forControlEvents:UIControlEventTouchUpInside];
 
        [_rightDetailView.reduceButton addTarget:self action:@selector(rightReduceButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _rightDetailView;
}

- (EGAdView *)adView{
    if (_adView == nil) {
        
        _adView = [[EGAdView alloc]init];
    }
    return _adView;
}


@end
